package com.lrm.biz.impl;

import com.lrm.biz.ICommentBiz;
import com.lrm.dao.CommentJPA;
import com.lrm.pojo.Comment;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2017/12/6.
 * @Modified By:
 */
@Service
public class CommentBizImpl implements ICommentBiz {

    @Autowired
    private CommentJPA commentJPA;

    @Override
    public Comment addComment(Comment comment) {
        Long id = comment.getParentComment().getId();
        if (id!=-1) {
            Comment one = commentJPA.findOne(id);
            Comment comment1 = new Comment();
            BeanUtils.copyProperties(one,comment1);
            comment.setParentComment(comment1);
        }else {
            comment.setParentComment(null);
        }
        comment.setCreateTime(new Date());
        return commentJPA.save(comment);
    }

    @Override
    public List<Comment> listComment(Long blogId) {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        return commentJPA.findByBlogIdAndParentCommentNull(blogId,sort);
    }
}
